package com.jinlia.show.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.modules.base.model.po.OrderPO;
import com.jinlia.show.modules.base.model.query.OrderQuery;
import com.jinlia.show.modules.base.system.entity.OrderRefund;
import com.jinlia.show.modules.base.system.service.OrderMapperService;
import com.jinlia.show.modules.base.wxapp.entity.PayOrder;
import com.jinlia.show.modules.system.model.forms.SysOrderForm;
import com.jinlia.show.modules.system.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderMapperService orderMapperService;


    @PostMapping("create")
    public ResponseEntity<PayOrder> createOrder(@RequestBody SysOrderForm sysOrderForm) {
        if (sysOrderForm.getUserId() == null && sysOrderForm.getMemberCardId() == null) {
            sysOrderForm.setUserId(1l);//游客下单
        }
        return ResponseEntity.ok(orderService.createOrder(sysOrderForm));
    }
    @PostMapping("page")
    public ResponseEntity<Page<OrderPO>> page(@RequestBody OrderQuery orderQuery) {
        Page<OrderPO> orderPOIPage = orderMapperService.pageList(orderQuery);
        if (!CollectionUtils.isEmpty(orderPOIPage.getRecords())) {
            orderPOIPage.getRecords().forEach(i -> {
                i.filedVO(i::getUserName);
            });
        }
        return ResponseEntity.ok(orderPOIPage);
    }

    @GetMapping("/{id}")
    public ResponseEntity<OrderPO> getById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(orderMapperService.getOrderPO(id));
    }

    /**
     * 取消订单
     *
     * @param refundOrder
     * @return
     */
    @PostMapping("/cancel")
    public ResponseEntity<OrderRefund> cancelOrder(@RequestBody OrderRefund refundOrder) {
        return ResponseEntity.ok(orderService.cancelOrder(refundOrder));
    }

    /**
     * 接单:订单确认
     */
    @GetMapping("/confirmByOrder/{id}")
    public void confirmOrder(@PathVariable("id") Long id) {
        orderService.confirmOrder(id);
    }

    /**
     * 制作完成:叫号取餐
     */
    @GetMapping("/callOrder/{id}")
    public ResponseEntity<Boolean> callOrder(@PathVariable("id") Long id) {
        return ResponseEntity.ok(orderService.callOrder(id));
    }

    /**
     * 取餐
     *
     * @param id
     * @return
     */
    @GetMapping("/takeOrder/{id}")
    public ResponseEntity<Boolean> takeOrder(@PathVariable("id") Long id) {
        return ResponseEntity.ok(orderService.takeOrder(id));
    }

    /**
     * 订单完成
     */
    @GetMapping("/finish/{id}")
    public ResponseEntity<Boolean> finishOrder(@PathVariable("id") Long id) {
        return ResponseEntity.ok(orderService.finishOrder(id));
    }

}
